package com.example.xm.service.impl;

import com.example.xm.entity.Imgsentity;
import com.example.xm.entity.Productentity;
import com.example.xm.entity.dto.ProductDto;
import com.example.xm.entity.dto.ProductQueryDto;
import com.example.xm.entity.vo.EcahrsVo;
import com.example.xm.entity.vo.ProductListVo;
import com.example.xm.mapper.ImgsMapper;
import com.example.xm.mapper.ProductMapper;
import com.example.xm.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
* @author tong
* @description 针对表【product】的数据库操作Service实现
* @createDate 2024-04-13 12:37:40
*/
@Service
public class ProductServiceImpl implements ProductService {
@Autowired(required = false)
    ProductMapper productMapper;
@Autowired(required = false)
    ImgsMapper imgsMapper;
@Transactional
    @Override
    public void add(ProductDto dto) {
        Productentity productentity = new Productentity();
        BeanUtils.copyProperties(dto,productentity);
        productMapper.insert(productentity);
    List<String> imgList = dto.getImgList();
    for (String url : imgList) {
        Imgsentity imgsentity = new Imgsentity();
        imgsentity.setImg(url);
        imgsentity.setProductId(productentity.getId());
        imgsMapper.insert(imgsentity);
    }
}

    @Override
    public PageInfo<Productentity> list(ProductQueryDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getSize());
        List<Productentity> list= productMapper.selectList(dto);
        PageInfo<Productentity> userentityPageInfo = new PageInfo<Productentity>(list);
        return userentityPageInfo;
    }

    @Override
    @Transactional
    public ProductListVo getById(Integer id) {
        Productentity productentity= productMapper.getById(id);
        ProductListVo productListVo = new ProductListVo();
        BeanUtils.copyProperties(productentity,productListVo);
     List<String> urls=   imgsMapper.selectList(id);
        productListVo.setImgList(urls);
        return productListVo;
    }

    @Override
    public void update(ProductDto dto) {
        Productentity productentity = new Productentity();
        BeanUtils.copyProperties(dto,productentity);
        productMapper.updateByPrimaryKeySelective(productentity);
        imgsMapper.delete(productentity.getId());
        List<String> imgList = dto.getImgList();
        if(imgList!=null){
            for (String url : imgList) {
                Imgsentity imgsentity = new Imgsentity();
                imgsentity.setImg(url);
                imgsentity.setProductId(productentity.getId());
                imgsMapper.insert(imgsentity);
            }
        }

    }
    @Override
    public void guoqiTime(){
    String time= LocalDate.now().getYear()+"-"+LocalDate.now().getMonthValue()+"-"+LocalDate.now().getDayOfMonth();
   productMapper.guoqiTime(time);
        System.out.println(time);
    }

    @Override
    public void delete(int[] intArray) {
        productMapper.delete(intArray);
    }

    @Override
    public List<Productentity> xiaoliang() {
        return productMapper.xiaoliang();
    }

    @Override
    public  List<EcahrsVo> echarts(Integer num,Integer type) {
        List<EcahrsVo> echars = productMapper.echars(num,type);
        for (EcahrsVo v : echars) {
            Productentity byId = productMapper.getById(v.getProductId());
            v.setName(byId.getTitle());
            BigDecimal jinjia = byId.getJinjia();
            BigDecimal price = byId.getPrice();
            BigDecimal subtract = price.subtract(jinjia);
            BigDecimal bigDecimal = BigDecimal.valueOf(v.getCount());
            BigDecimal multiply = subtract.multiply(bigDecimal);
            v.setShouyi(multiply);
        }
        return echars;
    }
}




